/**
 * 欢迎界面
 * by wyy
 */
class introScene extends GameObject implements eui.UIComponent {

    public displayGrp: eui.Group;
    public btnGrp: eui.Group;
    public startBtn: eui.Button;
    public watchBtn: eui.Button;
    public tipLbl: eui.Label;
    public nameLbl: eui.Label;
    public ruleBtn: eui.Button;

    public timeLbl: eui.Label;


    heightPercent = 1;
    widthPercent = 1;

    //是否已提示过名次
    showRank: boolean = false;

    type = "intro";

    constructor() {
        super();
        this.skinName = "resource/eui_skins/game_skins/introSceneSkin.exml";
    }
    addToStage() { }
    firstCreate() {
        globalObject.getInstance().showRank = true;
        this.nameLbl.text = globalObject.getInstance().player.Username;
        if (globalObject.getInstance().createIntroListener) {
            //中秋博饼
            nano.on("gameend", (data) => {
                if (SceneManager.getInstance().getCurrentScene().type == "intro") {
                    let scene = (<introScene>SceneManager.getInstance().getCurrentScene())
                    if (data.code == 1000) {
                        scene.startBtn.enabled = false;
                        scene.startBtn.label = "敬请期待";
                    }
                    else if (data.code == 10000) {
                        scene.startBtn.enabled = false;
                        scene.startBtn.label = "活动结束";
                    }
                    else {
                        scene.startBtn.enabled = true;
                        scene.startBtn.label = "开始博饼";
                    }
                    scene.setTipText(data.message);
                }
            });
            //中奖和未中奖的广播
            nano.on("rankresult", (data) => {
                let type = SceneManager.getInstance().getCurrentScene().type;
                if ((type == "intro") || (type == "hall") || (type == "result")) {
                    if (globalObject.getInstance().showRank) {
                        globalObject.getInstance().debugConsole("中奖或未中奖提示 监听rankresult", data);
                        SceneManager.getInstance().showWindow(new tipWindow({ text: data.msg, btnText: "确定" }));
                        globalObject.getInstance().showRank = false;
                    };
                }
            });
            //同一桌所有人都摇完 推送结果
            nano.on("cruover", (d) => {
                globalObject.getInstance().debugConsole("同一桌所有人都摇完 推送结果 监听cruover", d);
                let pInfo;
                let rank = 10;
                let result = d.result;
                let round = d.round;
                for (let i in result) {
                    if (result[i].id == globalObject.getInstance().player.Userid) {
                        pInfo = result[i];
                        rank = Number(i);
                    }
                }
                let text = "";
                if (rank + 1 <= 3) {
                    //中奖提示
                    if (round == 1) {
                        text = "恭喜你中奖了，快搓搓手准备第二轮吧！奖品请联系941117领取";
                    }
                    else if (round == 2) {
                        text = "哇塞！今天手气不错呀，第三轮继续给力嗷！奖品请联系941117领取";
                    }
                    else if (round == 3) {
                        text = "666，恭喜第三轮中奖！今天不去买彩票说的过去吗~奖品请联系941117领取";
                    }
                }
                else {
                    //未中奖提示
                    if (round == 1) {
                        text = "很抱歉您未中奖，期待明年你的好运！";
                    }
                    else if (round == 2) {
                        text = "哎呀就差一点点，很遗憾你止步于此哦！";
                    }
                    else if (round == 3) {
                        text = `很遗憾阁下在第3轮${globalObject.getInstance().player.Deskid}桌取得了第${rank}名，大奖和你擦身而过~`;
                    }
                }

                setTimeout(() => {
                    SceneManager.getInstance().showWindow(new tipWindow({ text: text, btnText: "确定" }))
                }, 1800);
            });
            globalObject.getInstance().createIntroListener = false;
        }

        this.startBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.joinDesk, this);
        this.watchBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            SceneManager.getInstance().changeScene(new hallScene());
        }, this);
        this.ruleBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,
            () => { SceneManager.getInstance().showWindow(new helpScene()) }, this);

    }

    onRemove() { }
    setTipText(s) {
        if (s.indexOf("/")) {
            let arr = s.split("/");
            this.timeLbl.text = arr[1];
            this.tipLbl.text = arr[0];
        }
        else {
            this.timeLbl.text = s;
        }
    }
    private joinDesk() {
        let id = loginScene.getInstance().playerInfo.id;
        globalObject.getInstance().debugConsole("申请加入桌子");
        nano.request("gamemanager.joindesk", {}, (data) => {
            globalObject.getInstance().debugConsole("当前桌子信息：", data);
            switch (data.code) {
                //加入成功
                case 200:
                    globalObject.getInstance().showTip(data.message);
                    loginScene.getInstance().playerInfo.deskid = data.data.desk_id;
                    SceneManager.getInstance().closeWindow();
                    SceneManager.getInstance().changeScene(new deskScene(data));
                    break;
                //其他情况
                default:
                    SceneManager.getInstance().showWindow(
                        new tipWindow({ text: data.message, btnText: "观战", func: this.intoHall }));
                    break;
            }

        });

    }
    private intoHall() {
        globalObject.getInstance().showTip("进入观战大厅成功！");
        SceneManager.getInstance().closeWindow();
        SceneManager.getInstance().changeScene(new hallScene());
    }

}